//
//  AIAddAddressController.m
//  aixianfeng
//
//  Created by 李江波 on 2016/12/22.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "AIAddAddressController.h"
#import "AIAddressModel.h"
#import "STPickerArea.h"
#import "NSObject+BoboHUD.h"

@interface AIAddAddressController ()<UITextFieldDelegate,STPickerAreaDelegate>
@property (weak, nonatomic) IBOutlet UIButton *manBotton;
@property (weak, nonatomic) IBOutlet UIButton *womanBotton;

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *telphoneNum;
@property (weak, nonatomic) IBOutlet UITextField *cityTextField;
@property (weak, nonatomic) IBOutlet UITextField *areaTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;


@end

@implementation AIAddAddressController{

    NSInteger telNum;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    telNum = 0;
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    
    self.nameTextField.text = self.model.name;
    self.manBotton.selected = self.model.isMan;
    self.womanBotton.selected = !self.model.isMan;
    self.telphoneNum.text = self.model.telphoneNum;
    self.cityTextField.text = self.model.city;
    self.areaTextField.text = self.model.area;
    self.addressTextField.text = self.model.address;
    
    [self.telphoneNum addTarget:self action:@selector(textFieldDidEditing:) forControlEvents:UIControlEventEditingChanged];
    self.cityTextField.delegate = self;
}

- (IBAction)clickMan:(UIButton *)sender {
    
    sender.selected = YES;
    self.womanBotton.selected = NO;
}
- (IBAction)clickWoman:(UIButton *)sender {
    
    sender.selected = YES;
    self.manBotton.selected = NO;
}

#pragma mark : - 保存
-(void)save{

    if (self.model == nil) {
        
        self.model = [[AIAddressModel alloc]init];
    }
    //判断内容是否为空
    if (self.nameTextField.text.length == 0) {
        
        [self showInfo:@"你没名字吗？"];
        return;
    }else if (self.telphoneNum.text.length <13){
        
        [self showInfo:@"电话号码啊"];
        return;
    }else if (self.cityTextField.text.length == 0){
        
        [self showInfo:@"你城市呢？"];
        return;
    }else if (self.areaTextField.text.length == 0){
        
        [self showInfo:@"具体地址"];
        return;
    }else if (self.addressTextField.text.length == 0){
        
        [self showInfo:@"你家门牌号"];
        return;
    }else{
        
        if (self.manBotton.selected == NO && self.womanBotton.selected == NO) {
            [self showInfo:@"人妖吗？"];
            return;
        }
    }
    
    
    _model.name = self.nameTextField.text;
    _model.isMan = self.manBotton.selected;
    _model.telphoneNum = self.telphoneNum.text;
    _model.city = self.cityTextField.text;
    _model.area = self.areaTextField.text;
    _model.address = self.addressTextField.text;
    
    if (self.saveBlock) {
        
        self.saveBlock(_model);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark : - 监听电话号码输入
-(void)textFieldDidEditing:(UITextField *)textField{

    if (textField == self.telphoneNum) {
        
        if (textField.text.length > telNum) {
            
            if (textField.text.length == 4 || textField.text.length == 9) {//输入
                NSMutableString *mutableStr = [[NSMutableString alloc]initWithString:textField.text];
                [mutableStr insertString:@" " atIndex:textField.text.length - 1];
                textField.text = mutableStr;
            }else if (textField.text.length >= 13){
                
                textField.text = [textField.text substringToIndex:13];
                [textField resignFirstResponder];
            }
            telNum = textField.text.length;
        }else if (textField.text.length < telNum){
        
            if (textField.text.length == 4 || textField.text.length == 9) {
                
                textField.text = [textField.text substringToIndex:textField.text.length - 1];
            }
            telNum = textField.text.length;
        }
        
    }
}

#pragma mark : - UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{

    if (textField == self.cityTextField) {
        
        [self.cityTextField resignFirstResponder];
        
        
        STPickerArea *pickerArea = [[STPickerArea alloc]init];
        [pickerArea setDelegate:self];
        [pickerArea setContentMode:STPickerContentModeBottom];
        [pickerArea show];
    }
}
- (void)pickerArea:(STPickerArea *)pickerArea province:(NSString *)province city:(NSString *)city area:(NSString *)area
{
    NSString *text = [NSString stringWithFormat:@"%@ %@ %@", province, city, area];
    self.cityTextField.text = text;
}




@end
